<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>LogonTracer</title>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.7/dist/css/bootstrap.min.css" integrity="sha384-LN+7fdVzj6u52u30Kp6M/trliBMCMKTyK833zpbD+pXdCLuTusPj697FH4R/5mcr" crossorigin="anonymous">
  <link rel="stylesheet" href="static/css/style.css">
  <link rel="stylesheet" href="static/css/dark-mode.css">
  <!-- Neo4j JavaScript Driver -->
  <script src="https://cdn.jsdelivr.net/npm/neo4j-driver@4.4.11/lib/browser/neo4j-web.min.js" integrity="sha384-M7IgiSpzRjF1qJu8pYaYwoDwXif/XBFUlabbZLNDtqXE7kPugcTx7QRyZqveSoBZ" crossorigin="anonymous"></script>
</head>

<body>
  <nav class="navbar navbar-expand-lg navbar-light bg-light p-1 shadow">
    <a class="navbar-brand" href="/"><img class="my_svg" src="static/images/logo_top.svg" alt="top" width="190"></a>
    <div class="collapse navbar-collapse">
    </div>
  </nav>

  <div class="container">
    <div class="row">
      <div class="col-md-8 offset-md-2">
        <div class="mt-8 p-8">
          <form action="" method="POST">
            <h4 class="text-center mt-3">User Management</h4>
            <div id="table_body"></div>
            <div class="float-end">
              <button type="submit" class="btn btn-secondary" name="action" value="active">Active</button>
              <button type="submit" class="btn btn-secondary" name="action" value="suspended">Suspend</button>
              <button type="submit" class="btn btn-secondary" name="action" value="delete">Delete</button>
              <a href="/" name="cancel" class="btn btn-dark">Cancel</a>
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>
  <script type="text/javascript">
    var users = new Array();
    var neo = neo4j.default;
    //Neo4j access settings
    if (location.protocol == 'http:') {
      var driver = neo.driver("bolt://{{ server_ip }}:{{ ws_port }}", neo.auth.basic("{{ neo4j_user }}", "{{ neo4j_password }}"));
    } else if (location.protocol == 'https:') {
      var driver = neo.driver("bolt+ssc://{{ server_ip }}:{{ ws_port }}", neo.auth.basic("{{ neo4j_user }}", "{{ neo4j_password }}"));
    }
    var session = driver.session({database: 'system'});
    session.run('SHOW USERS')
      .subscribe({
        onNext: record => {
          var userdata = new Array();
          userdata.push(record.get('user'));
          userdata.push(record.get('roles'));
          userdata.push(record.get('suspended'));
          users.push(userdata);
          //console.log(userdata);
        },
        onCompleted: () => {
          session.close()
          var tableMenu = '<table class="table table-hover"><thead><tr><th scope="col"></th><th scope="col">User</th><th scope="col">Role</th><th scope="col">Suspended</th></tr></thead><tbody>';
          for (i = 0; i < users.length; i++) {
            tableMenu +=  '<th><div class="form-check"><input class="form-check-input" type="checkbox" name="userlist" value="Check_' + users[i][0] + '"></div></th>';
            tableMenu +=  '<td>' + users[i][0] + '</td>';
            try {
              if (users[i][1].indexOf("admin") != -1) {
                tableMenu +=  '<td>admin</td>';
              } else {
                tableMenu +=  '<td>user</td>';
              }
            } catch (error) {
              //console.error(error);
              tableMenu +=  '<td>admin</td>';
            }
            tableMenu +=  '<td>' + users[i][2] + '</td></tr>';
          }
          tableMenu +=  '</tbody></table>';
          document.getElementById("table_body").innerHTML = tableMenu;
        },
        onError: error => {
          console.log(error)
        }
      })
  </script>
  <!-- Bootstrap JS -->
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.7/dist/js/bootstrap.min.js" integrity="sha384-7qAoOXltbVP82dhxHAUje59V5r2YsVfBafyUDxEdApLPmcdhBPg1DKg1ERo0BZlK" crossorigin="anonymous"></script>
</body>

</html>
